//
//  LYFirstTestController.m
//  SelectCity
//
//  Created by liuyan on 2019/3/19.
//  Copyright © 2019年 liuyan. All rights reserved.
//


#import "LYFirstTestController.h"
#import "AppDelegate.h"

#import "TConversationCell.h"
#import "TPopView.h"
#import "TPopCell.h"
#import "THeader.h"
//#import "IMMessageExt.h"
#import "TUIKit.h"
#import "TNaviBarIndicatorView.h"
#import "TIMFriendshipManager.h"

#import "ChatViewController.h"
#import "TSystemMessageCell.h"

@interface LYFirstTestController ()<UIGestureRecognizerDelegate, UITableViewDelegate, UITableViewDataSource, UIPopoverPresentationControllerDelegate>
@property (nonatomic, strong) NSMutableArray *data;
@property (nonatomic, strong) TNaviBarIndicatorView *titleView;
@property (nonatomic, assign) NSInteger managerDataIndex;

@property (nonatomic, strong) NSMutableArray *chatListArr;
@property (nonatomic, assign) NSInteger unReadCount;
@end

@implementation LYFirstTestController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self setupNavigation];
    
    self.chatListArr = [NSMutableArray arrayWithCapacity:0];
    self.data = [NSMutableArray arrayWithCapacity:0];
    self.unReadCount = 0;
    self.managerDataIndex = 0;
    [self setupViews];
}
- (void)setupNavigation {
    _titleView = [[TNaviBarIndicatorView alloc] init];
    [_titleView setTitle:@"消息"];
    self.navigationItem.titleView = _titleView;
    self.parentViewController.navigationItem.titleView = _titleView;
    UIButton *moreButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [moreButton setImage:[UIImage imageNamed:TUIKitResource(@"more")] forState:UIControlStateNormal];
    [moreButton addTarget:self action:@selector(rightBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *moreItem = [[UIBarButtonItem alloc] initWithCustomView:moreButton];
    self.navigationItem.rightBarButtonItem = moreItem;
    self.parentViewController.navigationItem.rightBarButtonItem = moreItem;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
- (void)setupViews {
    _tableView = [UIToolsControl initTableViewWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-HeightOfStatusBarAndNavigationBar-HeightOfTabBar) target:self style:UITableViewStylePlain];
    _tableView.tableFooterView = [[UIView alloc] init];
    _tableView.backgroundColor = COLOR_OF_WHITE;
    [self.view addSubview:_tableView];
    
    [self.noDataView vcNilStatusType:VCNilStatusTypeOfCommonNoData superView:self.view];
    self.noDataView.hidden = YES;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.data = [NSMutableArray arrayWithCapacity:0];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onRefreshConversations:) name:TUIKitNotification_TIMRefreshListener object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNetworkChanged:) name:TUIKitNotification_TIMConnListener object:nil];
//    [self updateConversations];
    [self checkLoginstatus];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
/*
- (void)updateConversations {
    _data = [NSMutableArray array];
    TIMManager *manager = [TIMManager sharedInstance];
    NSArray *convs = [manager getConversationList];
    for (TIMConversation *conv in convs) {
        if ([conv getType] == TIM_SYSTEM) {
            continue;
        }
        //[conv getMessage:[[TUIKit sharedInstance] getConfig].msgCountPerRequest last:nil succ:nil fail:nil];
        TIMMessage *msg = [conv getLastMsg];
        TConversationCellData *data = [[TConversationCellData alloc] init];
        data.unRead = [conv getUnReadMessageNum];
        data.time = [self getDateDisplayString:msg.timestamp];
        data.subTitle = [self getLastDisplayString:conv];
        if([conv getType] == TIM_C2C){
            data.head = TUIKitResource(@"default_head");
        } else if([conv getType] == TIM_GROUP) {
            data.head = TUIKitResource(@"default_group");
        }
        data.convId = [conv getReceiver];
        data.convType = (TConvType)[conv getType];
        
        if(data.convType == TConv_Type_C2C) {
            data.title = data.convId;
        } else if(data.convType == TConv_Type_Group) {
            data.title = [conv getGroupName];
        }
        [_data addObject:data];
    }
    [_tableView reloadData];
}
*/
- (void)checkLoginstatus {
    if (Signin) {
        if ([[TIMManager sharedInstance] getLoginStatus] == TIM_STATUS_LOGINED) {
            [self updateConversations];
        } else {
            [self gotoLogoin];
        }
    } else {
        [SharedApp showViewController:TabBarTypeOfSignin];
    }
}
- (void)gotoLogoin {
    [[TUIKit sharedInstance] initKit:[UD_GetOForK(isIMAppID) integerValue] accountType:UD_GetOForK(isIMAccountType) withConfig:[TUIKitConfig defaultConfig]];
    NSString *identifierStr = UD_GetOForK(isUserID);
    NSString *userSigStr = UD_GetOForK(isIMUserSig);
    NSString *appidAt3rdStr = UD_GetOForK(isIMAppID);
    if ((identifierStr && identifierStr.length) && (userSigStr && userSigStr.length) && (appidAt3rdStr && appidAt3rdStr.length)) {
        TIMLoginParam *login_param = [[TIMLoginParam alloc ]init];
        // identifier 为用户名，userSig 为用户登录凭证
        // appidAt3rd 在私有帐号情况下，填写与 sdkAppId 一样
        login_param.identifier = UD_GetOForK(isUserID);
        login_param.userSig = UD_GetOForK(isIMUserSig);
        login_param.appidAt3rd = UD_GetOForK(isIMAppID);
        [[TIMManager sharedInstance] login: login_param succ:^(){
            [DataToolsControl sharedManager].isTIMLoginSuccess = YES;
            NSLog(@"Init Succ");
            [self updateConversations];
            NSData *tokenData = UD_GetOForK(isDeviceToken);
            if (tokenData && tokenData.length) {
                TIMTokenParam *param = [[TIMTokenParam alloc] init];
#if DEBUG
                param.busiId = TIMPushDevID;
#else
                param.busiId = TIMPushReleaseID;
#endif
                [param setToken:UD_GetOForK(isDeviceToken)];
                [[TIMManager sharedInstance] setToken:param succ:^{
                    NSLog(@"-----> 上传 token 成功 ");
                } fail:^(int code, NSString *msg) {
                    NSLog(@"-----> 上传 token 失败 ");
                }];
            }
        } fail:^(int code, NSString * err) {
            [DataToolsControl sharedManager].isTIMLoginSuccess = NO;
            NSLog(@"Login Failed: %d->%@", code, err);
        }];
    }
}
- (void)updateConversations {
//    _data = [NSMutableArray array];
    
    self.unReadCount = 0;
    TIMManager *manager = [TIMManager sharedInstance];
    self.chatListArr = [NSMutableArray arrayWithCapacity:0];
    NSArray *convs = [manager getConversationList];
    [self.chatListArr addObjectsFromArray:convs];
    
    __weak __typeof(self)weakSelf = self;
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for (TIMConversation *conv in convs) {
        if ([conv getType] == TIM_SYSTEM) {
            continue;
        }
        [arr addObject:[NSString stringWithFormat:@"%@", [conv getReceiver]]];
    }
    
    [[TIMFriendshipManager sharedInstance] getUsersProfile:arr forceUpdate:YES succ:^(NSArray<TIMUserProfile *> *profiles) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        NSMutableArray *reloadArr = [NSMutableArray arrayWithCapacity:0];
        for (TIMUserProfile *profile in profiles) {
            NSLog(@"user=%@", profile);
            NSMutableArray *listArr = [NSMutableArray arrayWithCapacity:0];
            if ([profile.identifier isEqualToString:@"btpin_helper"]) {
                for (TIMConversation *tempConv in strongSelf.chatListArr) {
                    if ([[tempConv getReceiver] isEqualToString:profile.identifier]) {
                        [listArr addObject:tempConv];
                    }
                }
            }
            if (profile.role == 1) {
                for (TIMConversation *tempConv in strongSelf.chatListArr) {
                    if ([[tempConv getReceiver] isEqualToString:profile.identifier] && ![profile.identifier isEqualToString:@"btpin_helper"]) {
                        [listArr addObject:tempConv];
                    }
                }
            }
            
            if (listArr&&listArr.count) {
                for (TIMConversation *conv in listArr) {
                    if ([conv getType] == TIM_SYSTEM) {
                        continue;
                    }
                    //[conv getMessage:[[TUIKit sharedInstance] getConfig].msgCountPerRequest last:nil succ:nil fail:nil];
                    TIMMessage *msg = [conv getLastMsg];
                    TConversationCellData *data = [[TConversationCellData alloc] init];
                    data.unRead = [conv getUnReadMessageNum];
                    strongSelf.unReadCount += [conv getUnReadMessageNum];
                    data.time = [self getDateDisplayString:msg.timestamp];
                    data.subTitle = [self getLastDisplayString:conv];
                    data.convId = profile.identifier;
                    data.convType = (TConvType)[conv getType];
                    data.title = profile.nickname;
                    data.head = profile.faceURL;
                    data.selfSignature = @"";
                    if ([profile.identifier isEqualToString:@"btpin_helper"]) {
                        data.selfSignature = @"官方";
                        if (data.subTitle.length<1) {
                            data.subTitle = @"您有0条系统消息未查看";
                        }
                    } else {
                        if (profile.selfSignature && [profile.selfSignature isKindOfClass:[NSData class]]) {
                            data.selfSignature = [[NSString alloc] initWithData:profile.selfSignature encoding:NSUTF8StringEncoding];
                        }
                    }
                    [reloadArr addObject:data];
                }
            }
        }
        if (reloadArr&&reloadArr.count) {
            self.noDataView.hidden = YES;
            NSMutableArray *changeArr = [NSMutableArray arrayWithCapacity:0];
            for (NSInteger i = 0; i<reloadArr.count; i++) {
                TConversationCellData *changeData = reloadArr[i];
                if ([changeData.convId isEqualToString:@"btpin_helper"]) {
                    if (changeArr.count) {
                        [changeArr insertObject:changeData atIndex:0];
                    } else {
                        [changeArr addObject:changeData];
                    }
                } else {
                    [changeArr addObject:changeData];
                }
            }
            BOOL hasBtpinHelper = NO;
            for (NSInteger i = 0; i<reloadArr.count; i++) {
                TConversationCellData *changeData = reloadArr[i];
                if ([changeData.convId isEqualToString:@"btpin_helper"]) {
                    hasBtpinHelper = YES;
                }
            }
            if (!hasBtpinHelper) {
                TConversationCellData *dataBtpinHelper = [[TConversationCellData alloc] init];
                dataBtpinHelper.unRead = 0;
                dataBtpinHelper.subTitle = @"您有0条系统消息未查看";
                dataBtpinHelper.convId = @"btpin_helper";
                dataBtpinHelper.convType = TIM_C2C;
                dataBtpinHelper.head = @"https://api.btpin.com/assets/img/avatar_helper.png";
                dataBtpinHelper.title = @"官方小助手";
                dataBtpinHelper.selfSignature = @"官方";
                
                if (changeArr.count) {
                    [changeArr insertObject:dataBtpinHelper atIndex:0];
                } else {
                    [changeArr addObject:dataBtpinHelper];
                }
            }
            
            if (strongSelf.data.count) {
                [strongSelf.data removeAllObjects];
            }
            [strongSelf.data addObjectsFromArray:changeArr];
            [strongSelf.tableView reloadData];
        } else {
            self.noDataView.hidden = NO;
        }
        UITabBarController *tabBarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
        tabBarController.viewControllers[3].tabBarItem.badgeValue = (strongSelf.unReadCount)?[NSString stringWithFormat:@"%ld", (long)strongSelf.unReadCount]:nil;
    } fail:^(int code, NSString *msg) {
        NSLog(@"%@", msg);
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        if (code == 6014) {
            [strongSelf gotoLogoin];
        } else {
            [SharedApp showViewController:TabBarTypeOfSignin];
        }
    }];
}



- (void)onRefreshConversations:(NSNotification *)notification {
//    [self updateConversations];
    [self checkLoginstatus];
}
- (void)onNetworkChanged:(NSNotification *)notification {
    TNetStatus status = (TNetStatus)[notification.object intValue];
    switch (status) {
        case TNet_Status_Succ:
            [_titleView setTitle:@"消息"];
            [_titleView stopAnimating];
            break;
        case TNet_Status_Connecting:
            [_titleView setTitle:@"连接中..."];
            [_titleView startAnimating];
            break;
        case TNet_Status_Disconnect:
            [_titleView setTitle:@"消息(未连接)"];
            [_titleView stopAnimating];
            break;
        case TNet_Status_ConnFailed:
            [_titleView setTitle:@"消息(未连接)"];
            [_titleView stopAnimating];
            break;
        default:
            break;
    }
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_data&&_data.count) {
        return _data.count;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [TConversationCell getSize].height;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    TConversationCellData *data = _data[indexPath.row];
    if ([data.convId isEqualToString:@"btpin_helper"]) {
        return NO;
    } else {
        return YES;
    }
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    TConversationCellData *conv = _data[indexPath.row];
    [_data removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
    TIMConversationType type = TIM_C2C;
    if(conv.convType == TConv_Type_Group){
        type = TIM_GROUP;
    } else if(conv.convType == TConv_Type_C2C){
        type = TIM_C2C;
    }
    [[TIMManager sharedInstance] deleteConversation:type receiver:conv.convId];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    TConversationCellData *data = _data[indexPath.row];
    ChatViewController *chat = [[ChatViewController alloc] init];
    if ([data.convId isEqualToString:@"btpin_helper"]) {
        chat.isNotice = YES;
    } else {
        chat.isNotice = NO;
    }
    chat.isSessionPush = YES;
    chat.conversation = data;
    chat.job_id = @"";
    chat.job_cn = @"";
    chat.wage_cn = @"";
    chat.user_name = @"";
    chat.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:chat animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TConversationCell *cell  = [tableView dequeueReusableCellWithIdentifier:TConversationCell_ReuseId];
    if (!cell) {
        cell = [[TConversationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TConversationCell_ReuseId];
    }
    if (_data&&_data.count) {
        [cell setData:[_data objectAtIndex:indexPath.row]];
    }
    return cell;
}
- (void)setData:(NSMutableArray *)data {
    _data = data;
    [_tableView reloadData];
}
- (void)rightBarButtonClick:(UIButton *)sender {
//    if(_delegate && [_delegate respondsToSelector:@selector(conversationController:DidClickRightBarButton:)]){
//        [_delegate conversationController:self DidClickRightBarButton:sender];
//    }
}

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone;
}

//- (UIModalPresentationStyle)

- (NSString *)getLastDisplayString:(TIMConversation *)conv {
    NSString *str = @"";
    TIMMessageDraft *draft = [conv getDraft];
    if (draft) {
        for (int i = 0; i < draft.elemCount; ++i) {
            TIMElem *elem = [draft getElem:i];
            if([elem isKindOfClass:[TIMTextElem class]]){
                TIMTextElem *text = (TIMTextElem *)elem;
                str = [NSString stringWithFormat:@"[草稿]%@", text.text];
                break;
            } else {
                continue;
            }
        }
        return str;
    }
    
    TIMMessage *msg = [conv getLastMsg];
    if (msg.status == TIM_MSG_STATUS_LOCAL_REVOKED){
        if(msg.isSelf) {
            return @"你撤回了一条消息";
        } else {
            return [NSString stringWithFormat:@"\"%@\"撤回了一条消息", msg.sender];
        }
    }
    for (int i = 0; i < msg.elemCount; ++i) {
        TIMElem *elem = [msg getElem:i];
        if ([elem isKindOfClass:[TIMTextElem class]]) {
            TIMTextElem *text = (TIMTextElem *)elem;
            str = text.text;
            break;
        } else if ([elem isKindOfClass:[TIMCustomElem class]]) {
            TIMCustomElem *custom = (TIMCustomElem *)elem;
//            str = custom.ext;
//            str = custom.desc;
//            str = @"[自定义消息]";
            
            TSystemMessageCellData *systemData = [[TSystemMessageCellData alloc] init];
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:custom.data options:NSJSONReadingMutableLeaves error:nil];
            systemData.customModel = [[IMCustomModel alloc] init];
            [systemData.customModel setValuesForKeysWithDictionary:jsonDict];
            
            if ([systemData.customModel.type isEqualToString:@"namecard_exchange"]) { // 交换名片请求消息
                str = @"[交换名片]";
            } else if ([systemData.customModel.type isEqualToString:@"namecard"]) { // 收到名片消息
                str = @"[收到名片]";
            } else if ([systemData.customModel.type isEqualToString:@"resume"]) { // 收到简历消息
                str = @"[收到简历]";
            } else if ([systemData.customModel.type isEqualToString:@"resume_apply"]) { // 简历申请确认消息
                str = @"[简历申请确认]";
            } else if ([systemData.customModel.type isEqualToString:@"apply_video"]) { // 视频会话邀请消息
                str = @"[视频会话邀请]";
            } else if ([systemData.customModel.type isEqualToString:@"notice"]) { // 官方通知消息
//                str = systemData.customModel.title;
                str = [NSString stringWithFormat:@"您有%d条系统消息未查看", [conv getUnReadMessageNum]];
            } else if ([systemData.customModel.type isEqualToString:@"resume_info"]) { // 简历详情消息
                str = @"[简历详情]";
            } else if ([systemData.customModel.type isEqualToString:@"job_info"]) { // 职位详情消息
                str = @"[职位详情]";
            } else if ([systemData.customModel.type isEqualToString:@"location"]) { // 位置
                str = @"[位置]";
            } else if ([systemData.customModel.type isEqualToString:@"tip"]) { //
//                str = systemData.customModel.content;
                str = [NSString stringWithFormat:@"您有%d条系统消息未查看", [conv getUnReadMessageNum]];
            } else {
                str = @"[自定义消息]";
            }
            break;
        } else if ([elem isKindOfClass:[TIMImageElem class]]) {
            str = @"[图片]";
            break;
        } else if([elem isKindOfClass:[TIMSoundElem class]]) {
            str = @"[语音]";
            break;
        } else if([elem isKindOfClass:[TIMVideoElem class]]) {
            str = @"[视频]";
            break;
        } else if([elem isKindOfClass:[TIMFaceElem class]]) {
            str = @"[动画表情]";
            break;
        } else if ([elem isKindOfClass:[TIMFileElem class]]) {
            str = @"[文件]";
            break;
        } else if ([elem isKindOfClass:[TIMGroupTipsElem class]]) {
            TIMGroupTipsElem *tips = (TIMGroupTipsElem *)elem;
            switch (tips.type) {
                case TIM_GROUP_TIPS_TYPE_INFO_CHANGE: {
                    for (TIMGroupTipsElemGroupInfo *info in tips.groupChangeList) {
                        switch (info.type) {
                            case TIM_GROUP_INFO_CHANGE_GROUP_NAME: {
                                str = [NSString stringWithFormat:@"\"%@\"修改群名为\"%@\"", tips.opUser, info.value];
                            } break;
                            case TIM_GROUP_INFO_CHANGE_GROUP_INTRODUCTION: {
                                str = [NSString stringWithFormat:@"\"%@\"修改群简介为\"%@\"", tips.opUser, info.value];
                            } break;
                            case TIM_GROUP_INFO_CHANGE_GROUP_NOTIFICATION: {
                                str = [NSString stringWithFormat:@"\"%@\"修改群公告为\"%@\"", tips.opUser, info.value];
                            } break;
                            case TIM_GROUP_INFO_CHANGE_GROUP_OWNER: {
                                str = [NSString stringWithFormat:@"\"%@\"修改群主为\"%@\"", tips.opUser, info.value];
                            } break;
                            default:
                                break;
                        }
                    }
                } break;
                case TIM_GROUP_TIPS_TYPE_KICKED: {
                    NSString *users = [tips.userList componentsJoinedByString:@"、"];
                    str = [NSString stringWithFormat:@"\"%@\"将\"%@\"剔出群组", tips.opUser, users];
                } break;
                case TIM_GROUP_TIPS_TYPE_INVITE: {
                    NSString *users = [tips.userList componentsJoinedByString:@"、"];
                    str = [NSString stringWithFormat:@"\"%@\"邀请\"%@\"加入群组", tips.opUser, users];
                } break;
                default:
                    break;
            }
        } else {
            str = @"您收到一条消息";
            continue;
        }
    }
    return str;
}
- (NSString *)getDateDisplayString:(NSDate *)date {
    NSCalendar *calendar = [ NSCalendar currentCalendar ];
    int unit = NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear ;
    NSDateComponents *nowCmps = [calendar components:unit fromDate:[ NSDate date ]];
    NSDateComponents *myCmps = [calendar components:unit fromDate:date];
    NSDateFormatter *dateFmt = [[NSDateFormatter alloc ] init ];
    
    NSDateComponents *comp =  [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday fromDate:date];
    
    if (nowCmps.year != myCmps.year) {
        dateFmt.dateFormat = @"yyyy/MM/dd";
    } else {
        if (nowCmps.day==myCmps.day) {
            dateFmt.AMSymbol = @"上午";
            dateFmt.PMSymbol = @"下午";
            dateFmt.dateFormat = @"aaa hh:mm";
        } else if((nowCmps.day-myCmps.day)==1) {
            dateFmt.AMSymbol = @"上午";
            dateFmt.PMSymbol = @"下午";
            dateFmt.dateFormat = @"昨天";
        } else {
            if ((nowCmps.day-myCmps.day) <=7) {
                switch (comp.weekday) {
                    case 1:
                        dateFmt.dateFormat = @"星期日";
                        break;
                    case 2:
                        dateFmt.dateFormat = @"星期一";
                        break;
                    case 3:
                        dateFmt.dateFormat = @"星期二";
                        break;
                    case 4:
                        dateFmt.dateFormat = @"星期三";
                        break;
                    case 5:
                        dateFmt.dateFormat = @"星期四";
                        break;
                    case 6:
                        dateFmt.dateFormat = @"星期五";
                        break;
                    case 7:
                        dateFmt.dateFormat = @"星期六";
                        break;
                    default:
                        break;
                }
            } else {
                dateFmt.dateFormat = @"yyyy/MM/dd";
            }
        }
    }
    return [dateFmt stringFromDate:date];
}

@end




/*
#import "LYFirstTestController.h"
#import "LYFirstTestCell.h"
#import "LYFirstTestModel.h"
#import "LYSystemMessageController.h"

#import "LYPersonCheckMeController.h"

#import "LYCompanyCheckMeController.h"

@interface LYFirstTestController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation LYFirstTestController

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        NSArray *array = @[
                           @{@"avatar":@"",@"name":@"求职招聘小助手",@"detaile":@"",@"isNewmessage":@"N",@"type":@"1",@"count":@"4",@"currentMessage":@"",@"time":@"2018-03-20 14:20",@"company_name":@"官方",@"job_name":@""},
                           @{@"avatar":@"",@"name":@"孙赢",@"detaile":@"",@"isNewmessage":@"N",@"type":@"2",@"count":@"4",@"currentMessage":@"",@"time":@"2018-03-20 14:20",@"company_name":@"",@"job_name":@""}, @{@"avatar":@"",@"name":@"刘美玲",@"detaile":@"",@"isNewmessage":@"Y",@"type":@"3",@"count":@"0",@"currentMessage":@"不好意思，我们已经找到人啦",@"time":@"2018-03-20 14:20",@"company_name":@"美团网",@"job_name":@"人事主管"}];
        for (NSDictionary *dic in array) {
            LYFirstTestModel *model = [LYFirstTestModel mj_objectWithKeyValues:dic];
            [_dataArray addObject:model];
        }
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
}
- (void)initUI {
    self.tableview = [UIToolsControl initTableViewWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-HeightOfStatusBarAndNavigationBar-HeightOfTabBar) target:self style:UITableViewStyleGrouped];
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.backgroundColor = COLOR_OF_WHITE;
    
    [self.tableview registerClass:[LYFirstTestCell class] forCellReuseIdentifier:kCellIdentifier_LYFirstTestCell];
    [self.view addSubview:self.tableview];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LYFirstTestCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_LYFirstTestCell forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    LYFirstTestCell *cell = (LYFirstTestCell *)[tableView cellForRowAtIndexPath:indexPath];
    cell.hiddenDot = YES;
    
    LYFirstTestModel *model = self.dataArray[indexPath.row];
    if ([model.type integerValue] == 1) {
        LYSystemMessageController * sysVC = [[LYSystemMessageController alloc] init];
        sysVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:sysVC animated:YES];
    } else if([model.type integerValue] == 2) {
        LYPersonCheckMeController *meVC = [[LYPersonCheckMeController alloc] init];
        meVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:meVC animated:YES];
    } else {
        LYCompanyCheckMeController *companyVC = [[LYCompanyCheckMeController alloc] init];
        companyVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:companyVC animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001f;
}

@end

*/
